package com.jdjw.modules.migrantworkssign.controller;


import com.jdjw.common.ResponseResult;
import com.jdjw.modules.migrantworkssign.entity.MigrantWorksSign;
import com.jdjw.modules.migrantworkssign.entity.MigrantWorksSignSave;
import com.jdjw.modules.migrantworkssign.entity.MigrantWorksSignVo;
import com.jdjw.modules.migrantworkssign.mapper.MigrantWorksSignMapper;
import com.jdjw.modules.migrantworkssign.service.MigrantWorksSignService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.configurationprocessor.json.JSONException;
import org.springframework.boot.configurationprocessor.json.JSONObject;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 * 务工人员库 前端控制器
 * </p>
 *
 * @author fq
 * @since 2021-07-27
 */
@RestController
@RequestMapping("/migrantworkssign")
public class MigrantWorksSignController {


    @Autowired
    private MigrantWorksSignService migrantWorksSignService;

    @Autowired
    private MigrantWorksSignMapper migrantWorksSignMapper;

    @ApiOperation("务工人员信息-查询")
    @PostMapping("queryAll")
    public ResponseResult queryAll(MigrantWorksSignVo migrantWorksSignVo){
        return migrantWorksSignService.queryAll(migrantWorksSignVo);
    }


    @ApiOperation("务工人员信息-添加")
    @PostMapping("save")
    public ResponseResult save(MigrantWorksSignSave migrantWorksSignSave){
        ResponseResult rrt=new ResponseResult();
        try {
            MigrantWorksSign migrantWorksSign=new MigrantWorksSign();
            BeanUtils.copyProperties(migrantWorksSignSave,migrantWorksSign);
            //生成务工编号
            migrantWorksSign.setWorkerCode(UUID.randomUUID().toString().replaceAll("-",""));
            migrantWorksSign.setCreateDate(new Date());//获取当前时间
            Map<String, Object> stringObjectMap = migrantWorksSignMapper.selectCodeAndAid(migrantWorksSignSave.getDeptName());
            if(stringObjectMap!=null){
                migrantWorksSign.setAttribute01(stringObjectMap.get("aid").toString());//镇id
                migrantWorksSign.setAttribute02(stringObjectMap.get("code").toString());//村id
            }
            migrantWorksSignService.save(migrantWorksSign);
            rrt.setCode(200);
            rrt.setMsg("保存成功");
        } catch (BeansException e) {
            e.printStackTrace();
            rrt.setCode(500);
            rrt.setMsg("保存失败"+e.getMessage());
        }
        return rrt;
    }


    @ApiOperation("务工人员信息-编辑")
    @PostMapping("edit")
    public ResponseResult edit(@RequestBody String sqlEdit) throws JSONException {
        JSONObject json=new JSONObject(sqlEdit);
        return migrantWorksSignService.edit(json.getString("sqlEdit"));
    }

    @ApiOperation("务工人员信息-逻辑删除")
    @PostMapping("logicalDeletion")
    public ResponseResult logicalDeletion(String id){
    ResponseResult rrt=new ResponseResult();
        try {
            MigrantWorksSign migrantWorksSign=new MigrantWorksSign();
            migrantWorksSign.setId(id);
            migrantWorksSign.setDelFlag("0");//1否、0是
            migrantWorksSignService.updateById(migrantWorksSign);
            rrt.setMsg("删除成功");
            rrt.setCode(200);
        } catch (Exception e) {
            e.printStackTrace();
            rrt.setMsg("删除失败"+e.getMessage());
            rrt.setCode(500);
        }
        return rrt;
    }


    @ApiOperation("务工人员信息-查看")
    @PostMapping("{id}/preview")
    public ResponseResult preview(@PathVariable("id")String id){
        ResponseResult rrt=new ResponseResult();
        try {
            MigrantWorksSign byId = migrantWorksSignService.getById(id);
            rrt.setCode(200);
            rrt.setMsg("查看成功");
            rrt.setData(byId);
        } catch (Exception e) {
            e.printStackTrace();
            rrt.setCode(500);
            rrt.setMsg("查看失败"+e.getMessage());
            rrt.setData(null);
        }

        return rrt;
    }
}

